package com.entfrm.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entfrm.base.api.R;
import com.entfrm.base.config.GlobalConfig;
import com.entfrm.system.entity.BiInfo;
import com.entfrm.system.entity.BiLogin;
import com.entfrm.system.entity.ContractDetail;
import com.entfrm.system.entity.StockInfo;
import com.entfrm.system.service.BiInfoService;
import com.entfrm.system.service.BiLoginService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping( "/wms")
public class WmsController {

    @GetMapping("/wmsKc")
    public R list(Page page, StockInfo stockInfo) {
        Connection connection=null;
        Statement statement=null;
        Statement statement1=null;
        ResultSet resultSet = null;
        ResultSet resultSet1 = null;
        int pageCount=0;
        List<StockInfo> stockList=new ArrayList<>();
        try {
            connection = getConnectionWms();
            long s=(page.getCurrent()-1)*page.getSize()+1;
            String sqlCount="select  count(*) as data_count from VT_STOCKINFO where 1=1 ";
            String sql = "select  * from VT_STOCKINFO where  id between "+s+" and "+page.getCurrent()*(page.getSize());
            String condition="";
             if(stockInfo.getMaterialCode()!=null  && !("").equals(stockInfo.getMaterialCode())){
                       condition+=" AND F_MaterielCode like '%"+stockInfo.getMaterialCode()+"%'";
             }
            if(stockInfo.getMaterialName()!=null  && !("").equals(stockInfo.getMaterialName())){
                condition+=" AND C_Name like '%"+stockInfo.getMaterialName()+"%'";
            }
            if(stockInfo.getMaterilaSpec()!=null  && !("").equals(stockInfo.getMaterilaSpec())){
                condition+=" AND C_SPEC like '%"+stockInfo.getMaterilaSpec()+"%'";
            }
            if(stockInfo.getThOrBh()!=null  && !("").equals(stockInfo.getThOrBh())){
                condition+=" AND C_ThOrBh like '%"+stockInfo.getThOrBh()+"%'";
            }
            if(stockInfo.getDeptUse()!=null  && !("").equals(stockInfo.getDeptUse())){
                condition+=" AND C_DeptUse like '%"+stockInfo.getDeptUse()+"%'";
            }
            if(stockInfo.getPartUse()!=null  && !("").equals(stockInfo.getPartUse())){
                condition+=" AND   C_PartUse like '%"+stockInfo.getPartUse()+"%'";

            }
            statement = connection.createStatement();
            statement1 = connection.createStatement();
            resultSet1=statement.executeQuery(sqlCount+condition);
            while (resultSet1.next()) {
                 pageCount=resultSet1.getInt(1);
            }
            resultSet = statement1.executeQuery(sql+condition);
            while (resultSet.next()) {
                StockInfo stockInfo1=new StockInfo();
                 stockInfo1.setMaterialCode(resultSet.getString(1));
                 stockInfo1.setMaterialName(resultSet.getString(2));
                 stockInfo1.setMaterilaSpec(resultSet.getString(3));
                 stockInfo1.setmCount(resultSet.getString(4));
                 stockInfo1.setUnitCode(resultSet.getString(5));
                 stockInfo1.setUnitName(resultSet.getString(6));
                 stockInfo1.setClassCode(resultSet.getString(10));
                 stockInfo1.setClassName(resultSet.getString(11));
                 stockInfo1.setId(resultSet.getInt(12));
                 stockInfo1.setDeptUse(resultSet.getString(13)==null?"/":resultSet.getString(13));
                 stockInfo1.setThOrBh(resultSet.getString(14)==null?"/":resultSet.getString(14));
                 stockInfo1.setPjgys(resultSet.getString(15)==null?"/":resultSet.getString(15));
                 stockInfo1.setPartUse(resultSet.getString(16)==null?"/":resultSet.getString(16));
                 stockInfo1.setZdCbl(resultSet.getString(17)==null?"0":resultSet.getString(17));
               stockList.add(stockInfo1);
            }
        }catch (Exception e){
            e.printStackTrace();

        }finally {
            try {
                if(resultSet != null){
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(resultSet1 != null){
                    resultSet1.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(statement != null){
                    statement.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(statement1 != null){
                    statement1.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(connection != null){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return R.ok(stockList, pageCount);

    }
    public static Connection getConnectionWms() throws ClassNotFoundException, SQLException {
        String driverClass= GlobalConfig.sqlServerDriver();
        String url=GlobalConfig.wmsDataSourceUrl();
        String user=GlobalConfig.wmsUserName();
        String password=GlobalConfig.wmsPassword();
        Class.forName(driverClass);
        Connection conn = DriverManager.getConnection(url,user,password);
        return conn;
    }
}
